package com.bbg.amyam.web.util.view;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.text.WordUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.bbg.amyam.web.util.menu.MenuItem;

public class WebFlowViewFilesGenerator extends AbstractGenerator {

	private static final Log log = LogFactory
			.getLog(WebFlowViewFilesGenerator.class);

	public WebFlowViewFilesGenerator(MenuItem item, boolean overwrite) {
		super(item, overwrite);
	}

	public void execute() throws IOException {
		String viewTemplateDir = super.getViewTemplateDir() + "category"
				+ File.separator + "module" + File.separator + "webflow"
				+ File.separator;

		String targetParentDir = super.getViewDirPath() + "amyamviews"
				+ File.separator;
		String targetDir = targetParentDir + this.menuItem.getTarget()
				+ File.separator;
		
		String serviceClassName = this.menuItem.getId();
		serviceClassName = StringUtils.replace(serviceClassName, "_", " ");
		serviceClassName = WordUtils.capitalize(serviceClassName);
		serviceClassName = StringUtils.deleteWhitespace(serviceClassName);

		String serviceName = WordUtils.uncapitalize(serviceClassName);

		String moduleName = this.menuItem.getTargetParent();
		String flowName = this.menuItem.getId();

		this.replacedValues.put("__module__name__", moduleName);
		this.replacedValues.put("__flow_name__", flowName);
		this.replacedValues.put("__serviceName__", serviceName);
		this.replacedValues.put("__flow_target_name_", this.menuItem.getTarget());
		
		File srcDir = new File(viewTemplateDir);

		if(srcDir.exists()){
			log.debug("Found template dir:"+viewTemplateDir);
		}else{
			log.debug("Could not found template dir:"+viewTemplateDir);
		}
		Collection<File> files = FileUtils.listFiles(srcDir,
				new String[] {  "jspx", "xml"  }, false);

		for (File file : files) {
			String fileName = file.getName();
			String targetFileName = targetDir + fileName;
			log.debug("Copying  " + file.getPath() + File.separator + fileName
					+ " to " + targetFileName);
			File targetFile = new File(targetFileName);
			if (targetFile.exists()) {
				if (this.overwrite) {
					targetFile.delete();
				} else {
					log.debug("Skip copying " + file.getPath() + File.separator
							+ fileName + " to " + targetFileName);
					continue;
				}
			}
			copyFile(file, targetFile);
			log.debug("Copied  " + file.getPath() + File.separator + fileName
					+ " to " + targetFileName);
		}

	}

	private void copyFile(File src, File target) throws IOException {

		List<String> lines = FileUtils.readLines(src);
		ArrayList<String> newLines = new ArrayList<String>();
		boolean rmv = false;
		for (String line : lines) {
			String newLine = processLine(line);
			newLines.add(newLine);
		}

		FileUtils.writeLines(target, newLines);
	}
}
